﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using System.Web.UI.HtmlControls;
using GCELibrary.Current;
using ZF.Model;
using System.Transactions;
using ZF.BLL.Page;
//jun
namespace ZF.WS.UserControls
{
    public partial class EstateEdit : System.Web.UI.UserControl
    {
        public IEstateEditPage EstateEditPage { get { return (IEstateEditPage)this.Page; } }
        public void Page_Load(object sender, EventArgs e)
        {
        }
        public SiteInfo SiteInfo { get { return SiteInfo.Instance; } }
        public WarnFactory WarnFactory { get { return ((IWarnFactoryPage)this.Page).WarnFactory; } }
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.UrlReferrer != null)
                {
                    this.ViewState["fromUrl"] = string.Concat(Request.UrlReferrer);
                }
            }
            base.OnLoad(e);
        }
        public void InitControls()
        {
            //this.cblBaseFacility.DataSource = DictionaryConfig.HBaseFacility;
            //this.cblBaseFacility.DataBind();
            //this.cblWithFacility.DataSource = DictionaryConfig.HWithFacility;
            //this.cblWithFacility.DataBind();
            //this.rblDecoration.DataSource = DictionaryConfig.HDecoration;
            //this.rblDecoration.DataBind();
            //this.ddlDirection.DataSource = DictionaryConfig.HDirection;
            //this.ddlDirection.DataBind();
            this.ddlHouseType.DataSource = DictionaryConfig.HHouseType;
            this.ddlHouseType.DataBind();
            //this.ddlProperty.DataSource = DictionaryConfig.HProperty;
            //this.ddlProperty.DataBind();
            //var dt = HouseEditPage.DealType.ToString();
            //foreach (Control c in this.Controls)
            //{
            //    if (c is HtmlControl)
            //    {
            //        var hc = (HtmlControl)c;
            //        if (!string.IsNullOrEmpty(hc.Attributes["key"]))
            //        {
            //            hc.Visible = hc.Attributes["key"].Split(',').Contains(dt);
            //        }
            //    }
            //}
            var member = SiteInfo.GetCurrentMember();
            if (member != null && member.UserType == UserType.Admin)
            {
                this.cbJp.Visible = true;
            }
            else
            {
                this.cbJp.Visible = false;
            }
            //if (HouseEditPage.DealType == 2)
            //{
            //    this.cbHZ.Checked = true;
            //}
            //HouseEditPage.Type = (HouseEditPage.OperType == PageOperType.AddNew ? "发布" : "修改") + SiteInfo.GetHouseTitle(HouseEditPage.DealType);
        }
        public void Add()
        {
            InitControls();
            var member=SiteInfo.GetCurrentMember();
            //if(member!=null)
            //{
            //    this.tbContact.Text = member.FullName;
            //    this.tbPubEmail.Text = member.EMail;
            //    this.tbQQ.Text = member.QQ;
            //    this.tbPhone.Text = member.Phone;
            //}
        }
        public void AddSubmit(object sender, EventArgs e)
        {
            using (var ts = new TransactionScope())
            {
                using (var dc = SiteInfo.CreateDBDataContext())
                {
                    var estate = new Model.H_Estate()
                    {
                        AreaId1 = this.tbCity.Text.Convert<int>(),
                        AreaId2 = this.tbArea.Text.EmptyInt(),
                        Address = this.tbAddress.Text,
                        AddTime = DateTime.Now,
                        //Contact = this.tbContact.Text,
                        //QQ = this.tbQQ.Text,
                        Title = this.tbTitle.Text,
                        HouseType = this.ddlHouseType.SelectedValue,
                        //Phone = this.tbPhone.Text,
                        Description = this.tbDescription.Text,
                        Traffic = this.tbTraffic.Text,
                        UserArea = this.tbUserArea.Text,
                        ConstructArea = this.tbConstructArea.Text,
                        //Email = this.tbPubEmail.Text,
                        MemberId = SiteInfo.GetCurrentMember() != null ? SiteInfo.GetCurrentMember().MemberId : 0,
                        IsVerify = SiteInfo.SystemConfig.HousePubVerify,
                        Photo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";"),
                        SaleDate = this.tbSaleDate.Text.Convert<DateTime?>(default(DateTime?)),
                        Paperwork = this.tbPaperwork.Text,
                        Tel = this.tbTel.Text,
                        Price = this.tbUnitPrice.Text.Convert<int>(),
                        Office = this.tbOffice.Text,
                        DevUrl = this.tbDevUrl.Text,
                        Developers = this.tbDevelopers.Text,
                          IsRecommend=this.cbJp.Checked,
                    };
                    SiteInfo.CheckFilterWords(estate);
                    try
                    {
                        dc.H_Estate.InsertOnSubmit(estate);
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = "楼盘";

                    SiteInfo.InsertLog("发布" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功发布一个{0}，点击确定按钮返回上一页", msgKey));
                    link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                        string.Format("/House/estateInfo.aspx?hid={0}", estate.EstateId));
                    link.AddWarnMessageLink(string.Format("继续发布{0}", msgKey),
                        Request.Url.ToString());
                    var fromUrl = this.ViewState["fromUrl"];
                    if (fromUrl != null)
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, fromUrl.ToString()).Alert();
                    }
                    else
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, WarnLink.GoBack).Alert();
                    }
                }
            }
        }
        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var member = SiteInfo.GetCurrentMember();
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.LoginTimeOut);
                var sel = dc.H_Estate.Where(p => p.EstateId == EstateEditPage.HouseId);
                if (member.UserType != UserType.Admin)
                    sel = sel.Where(p => p.MemberId == member.MemberId);
                var house = sel.FirstOrDefault();
                if (house == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                
                InitControls(); ;
                this.tbCity.Text = house.AreaId1.ToString();
                this.tbArea.Text = house.AreaId2.ToString();
                this.tbAddress.Text = house.Address;
                
                //this.tbContact.Text = house.Contact;
                 
                //this.tbQQ.Text = house.QQ;
                //this.tbPhone.Text = house.Phone;
                //this.tbPubEmail.Text = house.Email;

                this.tbTitle.Text = house.Title;
         
                this.ddlHouseType.SelectedValue = house.HouseType.ToString();
     
                
                this.tbDescription.Text = house.Description;
                this.tbTraffic.Text = house.Traffic;
                this.tbUserArea.Text = string.Concat(house.UserArea);
                this.tbConstructArea.Text = string.Concat(house.ConstructArea);
 
                
                this.cbJp.Checked = house.IsRecommend;

                this.tbSaleDate.Text = house.SaleDate.HasValue ? house.SaleDate.Value.ToShortDateString() : "";
              this.tbPaperwork.Text = house.Paperwork;
              this.tbTel.Text = house.Tel;
              this.tbUnitPrice.Text = house.Price.ToString();
              this.tbOffice.Text = house.Office;
              this.tbDevUrl.Text = house.DevUrl;
              this.tbOffice.Text = house.Office;
              this.tbDevelopers.Text = house.Developers;





              this.tbPhoto.Text = house.Photo;
                



                this.lvImgList.DataSource = house.Photo.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();
 
              


            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {
            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var member = SiteInfo.GetCurrentMember();
                    if (member == null)
                        throw WarnFactory.CreateWarn(WarnCode.LoginTimeOut);
                    var sel = dc.H_Estate.Where(p => p.EstateId == EstateEditPage.HouseId);
                    if (member.UserType != UserType.Admin)
                        sel = sel.Where(p => p.MemberId == member.MemberId);
                    var house = sel.FirstOrDefault();
                    if (house == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);



                    house.AreaId1 = this.tbCity.Text.Convert<int>();
                    house.AreaId2 = this.tbArea.Text.EmptyInt();
                    house.Address = this.tbAddress.Text;


                    //house.Contact = this.tbContact.Text;

                    //house.QQ = this.tbQQ.Text;
                    //house.Phone = this.tbPhone.Text;
                    //house.Email = this.tbPubEmail.Text;
                    house.Title = this.tbTitle.Text;

                    house.HouseType = this.ddlHouseType.SelectedValue;

                    

                    house.Description = this.tbDescription.Text;
                    house.Traffic = this.tbTraffic.Text;
                    house.UserArea = this.tbUserArea.Text;
                    house.ConstructArea = this.tbConstructArea.Text;
                    house.IsRecommend = this.cbJp.Checked;

                    house.SaleDate = this.tbSaleDate.Text.Convert<DateTime?>(default(DateTime?));
                    house.Paperwork = this.tbPaperwork.Text;
                    house.Tel = this.tbTel.Text;
                    house.Price = this.tbUnitPrice.Text.Convert<int>();
                    house.Office = this.tbOffice.Text;
                    house.DevUrl = this.tbDevUrl.Text;
                    house.Developers = this.tbDevelopers.Text;
                    house.IsRecommend = this.cbJp.Checked;
                    



                    var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    house.Photo = photoList.ConstituteString(";"); ;


                    SiteInfo.CheckFilterWords(house);
                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = "楼盘";
                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改一个{0}，点击确定按钮返回{0}列表", msgKey));
                    link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                        string.Format("/House/EstateInfo.aspx?hid={0}", house.EstateId));
                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey),
                        Request.Url.ToString());
                    var fromUrl = this.ViewState["fromUrl"];

                    if (fromUrl != null)
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, fromUrl.ToString()).Alert();
                    }
                    else
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, WarnLink.GoBack).Alert();
                    }
                }
            }

        }
    }
}